import json
import sys
import os

sys.path.append("../")
sys.path.append(
    os.path.abspath(os.path.join(os.path.dirname("__file__"))))

from cognitive import rider_plan
from agentDesign.L1.read_map import AStarPlanner, ReadMap


# {
#   "rider_id": 0,
#   "rationality": 1,
#   "sensibility": 9,
#   "health": 10,
#   "dissatisfied": 5,
#   "rider_status": "down",
#   "home": [0, 0],
#
#   "richer_time": {},
#   "new_time_money": {},
#   "one_day_money":0,
#   "max_dic": {},
#   "least_dic": {},
#
#   "up_time": 30,
#   "sleep_time": 180
#
# }

#  取值范围：不满值 0-10、 理性+感性: 10, 体力值：最多12， 身体状况：up down，


def init_rider(rider_id, rationality, sensibility, health, rider_home, rider_money = None, rider_status = None):
    with open('./memories/one_shot.json') as f:
        example = json.load(f)

    with open('./memories/riders.json') as f2:
        members = json.load(f2)

    example["rider_id"] = rider_id
    example["rationality"] = rationality
    example["sensibility"] = sensibility
    example["rider_status"] = rider_status
    example["health"] = health
    example["home"] = rider_home
    example["money"] = rider_money
    members["members"].append(example)

    with open('./memories/riders.json', 'w') as file:
        json.dump(members, file)


def init_rider_llm(rider_id, rationality, sensibility, rider_status, health, rider_home):
    with open('./memories/one_shot.json') as f:
        example = json.load(f)

    with open('./memories/riders.json') as f2:
        members = json.load(f2)

    example["rider_id"] = rider_id
    example["rationality"] = rationality
    example["sensibility"] = sensibility
    example["rider_status"] = rider_status
    example["health"] = health
    example["home"] = rider_home

    members["members"].append(example)

    with open('./memories/riders.json', 'w') as file:
        json.dump(members, file)


def init_riders(rider_num, rider_ids, riders_rationality, riders_money = None, riders_status = None):
    #  初始化输入信息：个数， ids, 理性值， 初始金钱值, riders_status(up/down)
    random_status = True
    init_members = {"members": []}
    with open('./memories/riders.json', 'w') as file:
        json.dump(init_members, file)

    riders_sensibility = []
    # riders_status = []
    riders_health = []
    read_map = ReadMap()
    home_list = read_map.get_door("洋房")
    home_list.pop(1)

    if riders_money == None:
       riders_money = [0 for _ in range(int(rider_num))]
    if riders_status == None:
       riders_status = ["down" for _ in range(int(rider_num))]

    for i in range(len(riders_rationality)):
        riders_sensibility.append(10 - riders_rationality[i])
            # if random_status:
            #     riders_status.append("up")
            #     random_status = False
            # else:
            #     riders_status.append("down")
        riders_health.append(10)

    for i in range(int(rider_num)):
        init_rider(rider_ids[i], riders_rationality[i], riders_sensibility[i], riders_health[i],
                   home_list[int(i % 13)][0], riders_money[i], riders_status[i])


def init_riders_llm(rider_num, rider_ids, riders_rationality):
    random_status = True
    init_members = {"members": []}
    with open('./memories/riders.json', 'w') as file:
        json.dump(init_members, file)

    riders_sensibility = []
    riders_status = []
    riders_health = []
    read_map = ReadMap()
    home_list = read_map.get_door("洋房")
    home_list.pop(1)

    for i in range(len(riders_rationality)):
        riders_sensibility.append(10 - riders_rationality[i])
        if random_status:
            riders_status.append("up")
            random_status = False
        else:
            riders_status.append("down")
        riders_health.append(10)

    for i in range(rider_num):
        init_rider_llm(rider_ids[i], riders_rationality[i], riders_sensibility[i], riders_status[i], riders_health[i],
                       home_list[i][0])


if __name__ == "__main__":
    init_riders(9, [0, 1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8, 7],
                [1, 0], ["up", "down"])
    with open('../memories/riders.json') as f2:
        member_json = json.load(f2)
    for member in member_json["members"]:
        print(member)
        print(member["home"])
